package cn.imvc.app.controllers.order;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import cn.imvc.app.domains.OrderState;
import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.Member;
import cn.imvc.app.entities.Orders;
import cn.imvc.app.entities.SalesReturn;
import cn.imvc.app.entities.User;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.SalesReturnModel;
import cn.imvc.app.models.UserModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class SalesReturnControllerImpl implements SalesReturnController {
	static Logger logger = Logger.getLogger(SalesReturnControllerImpl.class);
	@Autowired
	SalesReturnModel salesReturnModel;
	@Autowired
	UserModel userModel;
	@Autowired
	LoggingModel loggingModel;

	public Page<SalesReturn> page;
	public Orders orders;
	public SalesReturn salesReturn;
	public ViewRequest viewRequest;
	public int id;
	public int[] ids;
	public User user;
	public Map<Byte, String> orderState = OrderState.elMap;
	public Map<String, String> salesReturnMap = new HashMap<String, String>();
	public Member member;
	public String userName;
	public Double returnPrice;
	public String[] images;
	
	
	public void setImages(String[] images) {
		this.images = images;
	}

	public void setReturnPrice(Double returnPrice) {
		this.returnPrice = returnPrice;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public void setSalesReturnModel(SalesReturnModel salesReturnModel) {
		this.salesReturnModel = salesReturnModel;
	}

	public void setPage(Page<SalesReturn> page) {
		this.page = page;
	}

	public void setOrders(Orders orders) {
		this.orders = orders;
	}

	public void setSalesReturn(SalesReturn salesReturn) {
		this.salesReturn = salesReturn;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public void setOrderState(Map<Byte, String> orderState) {
		this.orderState = orderState;
	}
	
	public void setUser(User user) {
		this.user = user;
	}

	public String search() {
		// TODO Auto-generated method stub
		return null;
	}

	public String index() {
		page=salesReturnModel.find(viewRequest);
		return "sales-return/index";
	}

	public String show() {
		// TODO Auto-generated method stub
		return null;
	}

	public String editNew() {
		// TODO Auto-generated method stub
		return null;
	}

	public String create() {
		// TODO Auto-generated method stub
		return null;
	}

	public String edit() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String username="";
		if (principal instanceof UserDetails) {
		   username = ((UserDetails)principal).getUsername();
		} else {
		   username = principal.toString();
		}
		user = userModel.get(username);
		int userId = user.getId();
		salesReturn = salesReturnModel.get(id);
		salesReturn.setUserId(userId);
		String[] goodsArray = salesReturn.getReturnDetail().split(",");
		for (String goodsDetail : goodsArray) {
			String[] goodsDetailArray = goodsDetail.split(":");
			salesReturnMap.put(goodsDetailArray[0], goodsDetailArray[1]);
		}
		return "sales-return/edit";
	}

	public String update() {
		//TODO 暂时不用
		return "";
	}
	
	public String pass(){
		salesReturn.setRefundAmount((int)(returnPrice*100));
		SalesReturn return1 = salesReturnModel.updatePass(salesReturn);
		if(return1 == null){
			return Render.json(new DialogBean("300","该订单状态或退货价格存在异常，不能审核","",""));
		}
		loggingModel.info(logger, "退款审核成功，订单编号："+return1.getOrderCode());
		return Render.json(new DialogBean("200", "审核成功","sales_return_detail"));
	}
	
	public String reject(){
		SalesReturn return1 = salesReturnModel.updateReject(salesReturn);
		if(return1 == null){
			return Render.json(new DialogBean("300","该订单状态异常，不能审核","",""));
		}
		loggingModel.info(logger, "退款审核失败，订单编号："+return1.getOrderCode());
		return Render.json(new DialogBean("200", "审核失败","sales_return_detail"));
	}

	public String destroy() {
		// TODO Auto-generated method stub
		return null;
	}

	public String destroyBatch() {
		// TODO Auto-generated method stub
		return null;
	}


	public String success() {
		SalesReturn return1 = salesReturnModel.updateSuccess(salesReturn);
		if(return1 == null){
			return Render.json(new DialogBean("300","该订单状态或退货价格存在异常，不能退货","",""));
		}
		loggingModel.info(logger, "退货成功，订单编号："+return1.getOrderCode());
		return Render.json(new DialogBean("200", "退货成功","sales_return_detail"));
	}

	public String fail() {
		SalesReturn return1 = salesReturnModel.updateFail(salesReturn);
		if(return1 == null){
			return Render.json(new DialogBean("300","该订单状态异常，不能退货","",""));
		}
		loggingModel.info(logger, "退货失败，订单编号："+return1.getOrderCode());
		return Render.json(new DialogBean("200", "退货失败","sales_return_detail"));
	}

	public String images(){
		SalesReturn salesReturn = salesReturnModel.get(id);
		String urls = salesReturn.getImageUrl();
		if(urls != null && !"".equals(urls)){
			images = urls.split(",");
		}
		return "sales-return/images";
	}
}
